/***********************************************
 * skycli.cpp 2011-08-15 22:56:00 
 *            swhors@naver.com
 *
 ***********************************************/

#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <signal.h>
#include <unistd.h>

using namespace std;

#include "skycli.h"
#include "runcli.h"

void printUsage( char * aProgName )
{
   printf( "                                   \n"\
           " Usage                             \n"\
           "                                   \n"\
           "  %s [server address] [server port]\n"\
           "                                   \n",
           aProgName );  
}

void signal_handler( int aSig )
{
   switch( aSig )
   {
   case SIGHUP:
      /* rehash the server */
      cout << PROG_NAME << " received SIGHUP..." << endl;
      stopcli();
      sleep( 2 );
      exit( 0);
      break;
   case SIGTERM:
      /* finalize the server */
      cout << PROG_NAME << " received SIGTERM..." << endl;
      stopcli();
      sleep( 2 );
      exit( 0 );
      break;
   case SIGKILL:
      cout << PROG_NAME << " received SIGKILL..." << endl;
      stopcli();
      sleep( 2 );
      exit( 0 );
      break;
   default :
      break;
   }
}

void SignalSet()
{
    signal( SIGSTOP, signal_handler );
    signal( SIGKILL, signal_handler );
    signal( SIGHUP , signal_handler );
    signal( SIGTERM, signal_handler );
}

int main( int aArgc, char * aArgv[] )
{
   int  sRC                    = 0;
   int  sPort                  = 0;
   char sAddress[NET_ADDR_LEN] = {0,};
   
   if( aArgc == 3 || aArgc == 1 )
   {
      if( aArgc == 1 )
      {
         /* default setting */
         snprintf( sAddress,
                   NET_ADDR_LEN,
                   "%s",
                   NET_DEF_ADDR );
         sPort = NET_DEF_PORT;
         
      }
      else
      {
         /* user set value */
         snprintf( sAddress,
                   NET_ADDR_LEN,
                   "%s",
                   aArgv[1] );
         sPort = atoi( aArgv[2] );
      }
      
      sRC = runcli( sAddress, sPort );
   }
   else
   {
      /* illegal usage */
      printUsage( aArgv[0] );
   }
   
   return sRC;
}
